/**
 * 后台功能
 * */


(function () {
})();


//获取功能列表
function getList(){
    var formData = {
        realname: realname, phone: phone,
        start: pages.start, limit: pages.limit,
    };
    $.ajax({
        type: apis.userIndex.method,
        url: apis.userIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//HTML
function getHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].phone + '</td>';
        h += '<td>' + d[i].uname + '</td>';
        h += '<td>' + d[i].realname + '</td>';
        h += '<td>' + d[i].sex_name + '</td>';
        if (d[i].head = '') {
            h += '<td><img src="' + d[i].head + '" height="50"/></td>';
        } else {
            h += '<td>-</td>';
        }
        h += '<td>' + d[i].address_now + '</td>';
        h += '<td>' + d[i].disable_name + '</td>';
        h += '<td>' + d[i].real_name + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="lay_edit(\'修改\',\'user-edit.html\', ' + d[i].id + ');">';
        h += '修改';
        h += '</a>';
        h += ' &nbsp; ';
        if (!d[i].disable) {
            h += '<a href="javascript:;" style="color: red;" onclick="able_no(' + d[i].id + ')">';
            h += '禁用';
            h += '</a>';
        } else {
            h += '<a href="javascript:;" style="color: green;" onclick="able_yes(' + d[i].id + ')">';
            h += '启用';
            h += '</a>';
        }
        h += '&nbsp;';
        h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += '删除';
        h += '</a>';
        h += '<br>';
        if (d[i].is_realname < 2) {
            h += '<a href="javascript:;" onclick="auth_ok(' + d[i].id + ');" style="color: green;">';
            h += '实名通过';
            h += '</a>';
            h += ' &nbsp; ';
            h += '<a href="javascript:;" onclick="auth_no(' + d[i].id + ');" style="color: red;">';
            h += '实名拒绝';
            h += '</a>';
        }
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//修改弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


////显示图片
//function showImg(e){
//    var h = '', i;
//    h += '<img src="' + e.domain + e.img + '"/>';
//    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
//    $('#bigShow').html(h).height(e.h);
//}


//头像抓图
function uploadHead(e) {
    var fileObj = $('#headUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //imageUpload({img: res.target.result});
        var link = res.target.result;
        var h = '';
        h += '<img src="' + link + '"/>';
        h += '<input type="hidden" id="head" value="' + link + '"/>';
        $('#headShow').html(h);
    }
}


//身份证前照抓图
function uploadQian(e) {
    var fileObj = $('#qianUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //imageUpload({img: res.target.result});
        var link = res.target.result;
        var h = '';
        h += '<img src="' + link + '"/>';
        h += '<input type="hidden" id="qian" value="' + link + '"/>';
        $('#qianShow').html(h);
    }
}


//身份证后照抓图
function uploadHou(e) {
    var fileObj = $('#houUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //imageUpload({img: res.target.result});
        var link = res.target.result;
        var h = '';
        h += '<img src="' + link + '"/>';
        h += '<input type="hidden" id="hou" value="' + link + '"/>';
        $('#houShow').html(h);
    }
}


//微信收款码
function uploadWx(e) {
    var fileObj = $('#wxUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //imageUpload({img: res.target.result});
        var link = res.target.result;
        var h = '';
        h += '<img src="' + link + '"/>';
        h += '<input type="hidden" id="wx" value="' + link + '"/>';
        $('#wxShow').html(h);
    }
}


//详情
function getOne(){
    if (!id) { return }
    $.ajax({
        type: apis.userOne.method,
        url: apis.userOne.url,
        data: { id: id, },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            var d = res.data;
            var h = '';
            $('#nick')[0].value = d.uname;
            $('#realname')[0].value = d.realname;
            $('#phone')[0].value = d.phone;
            if (d.sex == 2) {
                //$('#sex02').isChecked();
                $('#sex01')[0].checked = false;
                $('#sex02')[0].checked = true;
            } else {
                $('#sex01')[0].checked = true;
                $('#sex02')[0].checked = false;
            }
            if (d.head != '') {
                h = '<img src="/' + d.head + '"/>';
                h += '<input type="hidden" id="head" value="' + d.head + '"/>';
                $('#headShow').html(h);
            }
            $('#address_now')[0].value = d.address_now;
            $('#id_card')[0].value = d.id_card;
            if (d.id_front != '') {
                h = '<img src="/' + d.id_front + '"/>';
                h += '<input type="hidden" id="qian" value="' + d.id_front + '"/>';
                $('#qianShow').html(h);
            }
            if (d.id_back != '') {
                h = '<img src="/' + d.id_back + '"/>';
                h += '<input type="hidden" id="hou" value="' + d.id_back + '"/>';
                $('#houShow').html(h);
            }
            if (d.wx_account != '') {
                h = '<img src="/' + d.wx_account + '"/>';
                h += '<input type="hidden" id="wx" value="' + d.wx_account + '"/>';
                $('#wxShow').html(h);
            }
            $('#zfb_account')[0].value = d.zfb_account;
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//修改用户
function modify(){
    var formData = {
        id: id,
        nick: $('#nick').val(),
        realname: $('#realname').val(),
        phone: $('#phone').val(),
        sex: $('#sex').val(),
        head: $('#head').val(),
        address_now: $('#address_now').val(),
        id_card: $('#id_card').val(),
        id_qian: $('#qian').val(),
        id_hou: $('#hou').val(),
        wx_account: $('#wx').val(),
        zfb_account: $('#zfb_account').val(),
    };
    console.log(formData);
    if (formData.nick == '') {
        layerMsgErr('昵称必须的'); return
    }
    if (formData.realname == '') {
        layerMsgErr('姓名必须的'); return
    }
    if (formData.phone == '') {
        layerMsgErr('手机号必须的'); return
    }
    if (formData.sex == '') {
        layerMsgErr('性别必须的'); return
    }
    $.ajax({
        type: apis.userModify.method,
        url: apis.userModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            layerMsgOk('操作成功!');
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function lay_del(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.userDel.method,
            url: apis.userDel.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('删除成功!');
                setTimeout(function(){
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//审核通过
function auth_ok(v){
    var msg = '确认要 <b style="color: green; font-size: 20px;">通过</b> 吗？';
    layer.confirm(msg,function(index){
        authApi({id: v, auth: 2});
    });
}
//审核拒绝
function auth_no(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">拒绝</b> 吗？';
    layer.confirm(msg,function(index){
        authApi({id: v, auth: 1});
    });
}
//审核APPI
function authApi(e){
    $.ajax({
        type: apis.userAuth.method,
        url: apis.userAuth.url,
        data: { id: e.id, auth: e.auth },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            layerMsgOk('操作成功!');
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//禁用
function able_no(v){
    layer.confirm('确认要 <b style="color: red; font-size: 20px;">禁用</b> 吗？',
        function(index){
            able_api({id: v, able: 1});
        });
}
//启用
function able_yes(v){
    layer.confirm('确认要 <b style="color: green; font-size: 20px;">启用</b> 吗？',
        function(index){
            able_api({id: v, able: 0});
        });
}
//ableAPI
function able_api(e){
    $.ajax({
        type: apis.userAble.method,
        url: apis.userAble.url,
        data: { id: e.id, able: e.able },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            layerMsgOk('操作成功!');
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}